#include "Parser.h"



string Parser::add(string _param){
	Data _d(_param);
	this->fileInterface_ptr->add_record(_d);
	this->fileInterface_ptr->write_file();
	return "String added!";
}

string Parser::display(){
	this->fileInterface_ptr->print_records();
	return "";
}

string Parser::del(string _param){
	//Check if _param is a number!
	if( true ){
		this->fileInterface_ptr->del_record(atoi(_param.c_str())-1);
		this->fileInterface_ptr->write_file();
		return "";
	} else {
		return "Error! Please enter a number as parameter to delete!\n";
	}
}

string Parser::clear(){
	this->fileInterface_ptr->clear_file();
	return "Cleared file!";
}

string Parser::exit(){
	cout << "Bye bye! =)\n";
	return "exit";
}



string Parser::parse(string command){
	//Extract command and parameters
	int first_space = command.find(' ');
	string cmd = "";
	string param = "";
	if( first_space >= 0 ){//Can find delimiter ' '
		cmd = command.substr(0,first_space);
		param = command.substr(first_space+1,command.length());
	} else {
		cmd = command.substr(0,command.size());
	}


	//cout << "Command: " << cmd.c_str() << "\nParameter(s): " << param.c_str() << "\n";

	//Route command and parameters to respective functions to handle
	if( strcmp(cmd.c_str(),"add") == 0){
		return this->add(param);
	} else if( strcmp(cmd.c_str(),"display") == 0){
		return this->display();
	} else if( strcmp(cmd.c_str(),"delete") == 0){
		return this->del(param);
	} else if( strcmp(cmd.c_str(),"clear") == 0){
		return this->clear();
	} else if( strcmp(cmd.c_str(),"exit") == 0){
		return this->exit();
	} else {
		return "Invalid Command!";
	}


}

void Parser::set_fileInterface(FileInterface* _FI){
	this->fileInterface_ptr = _FI;
}

FileInterface* Parser::get_fileInterface(){
	return this->fileInterface_ptr;
}
